package cn.itcast.aspect;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Arrays;

@Component
//@Aspect
public class RemoveTrimAdvice {
    @Around("execution(* cn.itcast.controller.*Controller.*(..))")
    public Object method(ProceedingJoinPoint pjp) throws Throwable {

        Object[] args = pjp.getArgs();

        System.out.println("args:" + Arrays.toString(args));

        for (int i = 0; i < args.length; i++) {
            if (args[i] instanceof String) {

                args[i] =  ((String) args[i]).trim();

                //System.out.println(arg);

                /*String str = (String) arg;
                String trim = str.trim();
                arg = trim;*/
            }else {
                Field[] declaredFields = args[i].getClass().getDeclaredFields();
                for (Field field : declaredFields) {
                    field.setAccessible(true);
                    if (field.getType() == String.class) {
                        //如果字段类型是String类型，就进行trim操作
                        String value = (String) field.get(args[i]);
                        String trim = value.trim();
                        field.set(args[i], trim);
                    }
                }
            }
        }
        return pjp.proceed(args);
        /*for (Object arg : args) {
            if (arg instanceof String) {

                arg =  ((String) arg).trim();

                System.out.println(arg);

                *//*String str = (String) arg;
                String trim = str.trim();
                arg = trim;*//*
            } else {
                Field[] declaredFields = arg.getClass().getDeclaredFields();
                for (Field field : declaredFields) {
                    field.setAccessible(true);
                    if (field.getType() == String.class) {
                        //如果字段类型是String类型，就进行trim操作
                        String value = (String) field.get(arg);
                        String trim = value.trim();
                        field.set(arg, trim);
                    }
                }
            }
        }*/


    }

}
